package com.hjq.demo.ui.helpers;

import com.hjq.http.EasyLog;
import com.umeng.commonsdk.debug.E;

import java.util.ArrayList;
import java.util.List;

public class MySocketClientManager {

    private List<MySocketClient> sockets = new ArrayList<>();
    private List<String> switchTop = new ArrayList<>();
    private List<String> switchGallery = new ArrayList<>();
    private List<String> switchTv = new ArrayList<>();

    public MySocketClientManager() {

        switchTop.add("192.168.1.200");

        switchGallery.add("192.168.1.201");
        switchGallery.add("192.168.1.202");
        switchGallery.add("192.168.1.203");
        switchGallery.add("192.168.1.204");
        switchGallery.add("192.168.1.205");
        switchGallery.add("192.168.1.206");
        switchGallery.add("192.168.1.207");

        switchTv.add("192.168.1.210");
        switchTv.add("192.168.1.211");
        switchTv.add("192.168.1.212");
        switchTv.add("192.168.1.213");
        switchTv.add("192.168.1.214");
        switchTv.add("192.168.1.215");
        switchTv.add("192.168.1.216");

    }

    private MySocketClient get(String url) {
        for (MySocketClient s : sockets) {
            if (StringUtil.isNotEmpty(s.getUrl()) && s.getUrl().equals(url)) {
                return s;
            }
        }
        return null;
    }

    public boolean connect(String ip) {
        try {
            String url = String.format("%s", ip);
            MySocketClient socket = this.get(url);
            if (socket == null) {
                socket = new MySocketClient();
                this.sockets.add(socket);
            } else {
                socket.disconnect();
                socket.connect(ip, 80);
            }
        } catch (Exception e) {
            EasyLog.print(e.getMessage());
            return false;
        }
        return true;
    }

    public boolean isConnected(String ip) {
        MySocketClient socket = this.get(ip);
        if (socket == null) return false;
        return socket.isConnected();
    }

    public boolean isTurn(String ip) {
        MySocketClient socket = this.get(ip);
        if (socket == null) return false;
        return socket.isTurn();
    }

    public boolean disconnect() {
        try {
            for (MySocketClient s : sockets) {
                s.disconnect();
            }
        } catch (Exception e) {
            EasyLog.print(e);
            return false;
        }
        return true;
    }

    public boolean send(String ip, String message) {
        try {
            MySocketClient s = get(ip);
            if (s.isConnected() == false) {
                s.reconnect();
            }
            return s.send(message);
        } catch (Exception e) {
            EasyLog.print(e.getMessage());
            return false;
        }
    }

    public boolean sendAll(String message) {
        try {
            for (MySocketClient s : sockets) {
                if (s.isConnected() == false) {
                    s.reconnect();
                }
                s.send(message);
            }
        } catch (Exception e) {
            EasyLog.print(e);
            return false;
        }
        return true;
    }

    public String getStatus() {
        StringBuilder sb = new StringBuilder();
        for (MySocketClient s : sockets) {
            sb.append(s.getStatus() + "\r\n");
        }
        return sb.toString();
    }


    public boolean connectAll() {
        try {
            for (String ip : switchTop) {
                if (this.isConnected(ip) == false) {
                    this.connect(ip);
                }
            }
            for (String ip : switchGallery) {
                if (this.isConnected(ip) == false) {
                    this.connect(ip);
                }
            }
            for (String ip : switchTv) {
                if (this.isConnected(ip) == false) {
                    this.connect(ip);
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean reConnectAll() {
        try {
            for (String ip : switchTop) {
                this.connect(ip);
            }
            for (String ip : switchGallery) {
                this.connect(ip);

            }
            for (String ip : switchTv) {
                this.connect(ip);
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean turnOne(String ip, String cmd) {
        return this.send(ip, cmd);
    }

    /**
     * 0 = top, 1= gallery, 2= tv
     *
     * @param type
     * @return
     */
    public boolean turnAll(int type, String cmd) {
        try {
            List<Boolean> allResult = new ArrayList<>();
            if (type == 0) {
                for (String ip : switchTop) {
                    allResult.add(this.send(ip, cmd));
                }
                for (String ip : switchGallery) {
                    allResult.add(this.send(ip, cmd));
                }
                for (String ip : switchTv) {
                    allResult.add(this.send(ip, cmd));
                }
            } else if (type == 1) {
                for (String ip : switchTop) {
                    allResult.add(this.send(ip, cmd));
                }
            } else if (type == 2) {
                for (String ip : switchGallery) {
                    allResult.add(this.send(ip, cmd));
                }
            } else if (type == 3) {
                for (String ip : switchTv) {
                    allResult.add(this.send(ip, cmd));
                }
            }
            boolean hasOneOk = false;
            for (boolean ok : allResult) {
                if (ok) {
                    hasOneOk = true;
                    break;
                }
            }
            if (hasOneOk == false) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean turnStatusAll(int type) {
        try {
            if (type == 1) {
                for (String ip : switchTop) {
                    if (isConnected(ip) && isTurn(ip))
                        return true;
                }
            } else if (type == 2 || type == 0) {
                for (String ip : switchGallery) {
                    if (isConnected(ip) && isTurn(ip))
                        return true;
                }
            } else if (type == 3) {
                for (String ip : switchTv) {
                    if (isConnected(ip) && isTurn(ip))
                        return true;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    private static class SingletonHolder {
        public final static MySocketClientManager instance = new MySocketClientManager();
    }

    public static MySocketClientManager getInstance() {
        return SingletonHolder.instance;
    }
}
